<?php

namespace app\wechat\service;

use app\admin\service\BaseService;
use app\wechat\model\AuthTokenModel;

class AuthTokenService extends BaseService
{
    protected $expireTime = 86400;

    /**
     * 生成微信登陆token
     *
     * @Author tanruizheng
     * @DateTime 2021-08-10
     * @param [type] $appid
     * @param [type] $openid
     * @param string $accountType
     * @return array
     */
    public function createAuthToken($appid, $openid, $accountType = 'office'){
        $authTokenModel = new AuthTokenModel();
        $authTokenModel->app_id = $appid;
        $authTokenModel->app_account_type = $accountType;
        $authTokenModel->open_id = $openid;
        $authTokenModel->code = md5($appid . time() . rand(10000,99999));
        $authTokenModel->token = md5($appid . time() . rand(10000,99999));
        $authTokenModel->expire_time = time() + $this->expireTime;
        $authTokenModel->refresh_token = md5($appid . time() . rand(10000,99999));
        $authTokenModel->save();
        return $authTokenModel->toArray();
    }
}